<?php


namespace tmcore\services;

use think\facade\Config;
use tmcore\utils\File;

class SysClearService
{
    /**
     * 获取系统包含的所有模块
     * @return array
     */
    public function getSysModule() {

        $app_map = Config::get('app.app_map');
        $modules = ['',];

        foreach ($app_map as $key => $value) {
            if($key == '*') continue;
            array_push($modules, $value);
        }

        return $modules?$modules:['','admin','api','pc','http'];
    }

    /**
     * 清除缓存
     */
    public function clearCache() {

        $root_path = root_path();
        $sys_module = $this->getSysModule();

        foreach ($sys_module as $key => $value) {
            $path = 'runtime/' . ($value?($value . '/'):'');
            File::del_dir($root_path . $path . 'session'); //删除session
            File::del_dir($root_path . $path . 'temp'); //删除临时模板
            File::del_file($root_path . $path . 'route.php'); //删除路由缓存
            File::del_file($root_path . $path . 'schema'); //删除字段缓存
        }
    }

    /**
     * 删除日志
     * @param int $day 日志保留天数，默认保留30天
     */
    public function deleteLog($day = 30) {

        $root_path = root_path();
        $sys_module = $this->getSysModule();

        foreach ($sys_module as $key => $value) {
            $path = 'runtime/' . ($value?($value . '/'):'');
            File::del_day_file($root_path . $path . 'log', $day);
        }
    }

    /**
     * 删除临时文件
     */
    public function deleteTempFile() {
        // 删除系统临时存储的文件
        File::del_dir(root_path().'public/uploads/temp');
    }
}